package com.example.myapplication.Cat;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.ShuJu.ManagementBean;
import com.example.myapplication.ShuJu.ParkingDtailsBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ParkingDtailsActivity extends AppCompatActivity {

    private  Gson gson ;
    private  Handler handler ;
    private  OkHttpClient okHttpClient ;
    private String listID;
    private TextView cat_name;
    private TextView cat_dizi;
    private TextView cat_juli;
    private TextView cat_kaifan;
    private TextView cat_shoufei;
    private TextView cat_cewei;
    private TextView cat_cankao;
    private Request request;
    private ParkingDtailsBean parkingDtailsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_dtails);
        Intent intent = getIntent();
        listID = intent.getStringExtra("listID");
        initview();
        sendbean();
    }

    private void sendbean() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/park/lot/"+listID)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                parkingDtailsBean = gson.fromJson(string, ParkingDtailsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onclikbean();
                    }
                });
            }
        });
    }

    private void onclikbean() {
        cat_name.setText("停车场名称 "+parkingDtailsBean.getData().getParkName());
        cat_dizi.setText("地址"+parkingDtailsBean.getData().getAddress());
        if(parkingDtailsBean.getData().getOpen().equals("Y")){
            cat_kaifan.setText("是否对外开放: 是");
        }else if(parkingDtailsBean.getData().getOpen().equals("N")){
            cat_kaifan.setText("是否对外开放: 否");
        }
        cat_cewei.setText("剩余车位:"+parkingDtailsBean.getData().getVacancy());
        cat_cankao.setText("收费参考: 每小时:"+parkingDtailsBean.getData().getRates()+"元，最高"+parkingDtailsBean.getData().getPriceCaps()+"元/天");
        cat_juli.setText("距离:"+parkingDtailsBean.getData().getDistance());
        cat_shoufei.setText("停车费: "+parkingDtailsBean.getData().getRates()+"元/小时");
    }

    private void initview() {
        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
        cat_name = findViewById(R.id.cat_name);
        cat_dizi = findViewById(R.id.cat_dizi);
        cat_juli = findViewById(R.id.cat_juli);
        cat_kaifan = findViewById(R.id.cat_kaifan);
        cat_shoufei = findViewById(R.id.cat_shoufei);
        cat_cewei = findViewById(R.id.cat_cewei);
        cat_cankao = findViewById(R.id.cat_cankao);

    }
}